package cn.chair.dao;

import cn.chair.entity.WorkOrder;
import cn.chair.model.admin.WorkOrderResp;
import life.innovation.dao.EntityDaoImpl;
import life.innovation.exception.DataReadException;
import life.innovation.model.PageList;
import org.springframework.stereotype.Repository;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository
public class WorkOrderDao extends EntityDaoImpl<WorkOrder> {

    public WorkOrderDao(DataSource dataSource) {
        super(dataSource, WorkOrder.class);
    }

    public PageList<WorkOrderResp.WorkOrderList> getWorkOrders(Integer workId, String startDate, String endDate, String description,
                                                                String userAccount, Integer userId,int page,int countPerPage ) throws DataReadException {


        Map<String,Object> params = new HashMap<>();
        startDate = (startDate == null||startDate.trim().isEmpty())?null:startDate.trim();
        endDate = (endDate == null||endDate.trim().isEmpty())?null:endDate.trim();
        description = (description == null||description.trim().isEmpty())?null:"%"+description.trim()+"%";
        userAccount = (userAccount==null||userAccount.trim().isEmpty())?null:userAccount.trim();

        params.put("workId",workId);
        params.put("startDate",startDate);
        params.put("endDate",endDate);
        params.put("description",description);
        params.put("userAccount",userAccount);
        params.put("userId",userId);

        return findPage("db/sql/work/getWorkOrders.sql",WorkOrderResp.WorkOrderList.class,page,countPerPage,params);
    }


}
